package com.ludomancy.sos64.components;

import java.awt.Graphics;
import java.awt.Point;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.sos64.Component;
import com.ludomancy.sos64.Game;
import com.ludomancy.sos64.Sos64App;

public class Driller extends Component
{
	private ImageAnimation driller;
//	private Accumulator drillTimer = new Accumulator(4000);
	private boolean isDrilling = false;
	
	public Driller(Point pos)
	{
		super(pos.x,pos.y);
		if(driller == null)
		{
			driller = new ImageAnimation(new ImageStrip(ResourceManager.getImage("tremor.png"), 43, 101), 3);
			driller.reverse();
			driller.setSequenceBouncingLoop();
		}
	}
	
	public void onMoving()
	{
		isDrilling = false;
//		drillTimer.reset();
		driller.stop();
		driller.rewind();
	}
	
	public void onMoved()
	{
		Point tile = Game.game.tileFor(getHotspot());
		// driller can only drill on valid shores
		if(Game.game.terrain.isBreakable(tile.x, tile.y))
		{
			isDrilling = true;
			Sound.play("start.wav");
		}
	}

	public boolean snapToTile()
	{
		return false;
	}
	
	public Point getHotspot()
	{
		return new Point(x+21,y+74);
	}
	
	public Point getSize()
	{
		return new Point(43,101);
	}
	
	public boolean canMove()
	{
		return true;
	}

	public void paint(Graphics g, int px, int py)
	{
		driller.paintCurrentFrame(g, px, py);
	}
	
	public boolean isMadeOfMetal()
	{
		return true;
	}


	public void update()
	{
		driller.update();
		if(isDrilling)
		{
			driller.play();
//			drillTimer.update();
			if(driller.isOver())
			{
				// tile broken!
				Sound.play("vault.wav");
				Game.game.nukeTile(Game.game.tileFor(new Point(x,y)));
				driller.stop();
//				drillTimer.reset();
				isDrilling = false;
				Sos64App.mainWidget.world.startShake();
				y += Game.TILE_HEIGHT/4;
			}
		}
		else
		{
			driller.stop();
		}
	}
}
